package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduCertificateDTO;
import com.zenith.edu.po.vo.EduCertificateVO;
import com.zenith.edu.service.EduCertificateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/certificate")
@Tag(name = "证书管理", description = "证书管理接口")
public class EduCertificateController {
    
    @Autowired
    private EduCertificateService eduCertificateService;
    
    @SecurePostMapping(value = "createCertificate",roles = {"ADMIN"},permission = "certificate:create")
    @Operation(summary = "创建证书")
    public Result<EduCertificateVO> createCertificate(@RequestBody EduCertificateDTO dto) {
        EduCertificateVO certificateVO = eduCertificateService.createCertificate(dto);
        return Result.success(certificateVO);
    }
    
    @SecurePostMapping(value = "/getCertificateById/{id}",roles = {"ADMIN"},permission = "certificate:read")
    @Operation(summary = "根据ID获取证书详情")
    public Result<EduCertificateVO> getCertificateById(@PathVariable Long id) {
        EduCertificateVO certificateVO = eduCertificateService.getCertificateById(id);
        if (certificateVO == null) {
            return Result.fail("证书不存在");
        }
        return Result.success(certificateVO);
    }
    
    @SecurePostMapping(value = "updateCertificate",roles = {"ADMIN"},permission = "certificate:update")
    @Operation(summary = "更新证书信息")
    public Result<EduCertificateVO> updateCertificate(@RequestBody EduCertificateDTO dto) {
        EduCertificateVO certificateVO = eduCertificateService.updateCertificate(dto);
        return Result.success(certificateVO);
    }
    
    @SecurePostMapping(value = "/deleteCertificate/{id}",roles = {"ADMIN"},permission = "certificate:delete")
    @Operation(summary = "删除指定证书")
    public Result<Void> deleteCertificate(@PathVariable Long id) {
        boolean result = eduCertificateService.deleteCertificate(id);
        if (!result) {
            return Result.fail("证书删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/getCertificateList",roles = {"ADMIN"},permission = "certificate:read")
    @Operation(summary = "获取证书列表")
    public Result<List<EduCertificateVO>> getCertificateList() {
        List<EduCertificateVO> certificateList = eduCertificateService.getCertificateList();
        return Result.success(certificateList);
    }
}